package com.yysjyr.logisticssystem.settings.controller;

import com.yysjyr.logisticssystem.common.ReturnObject;
import com.yysjyr.logisticssystem.common.contants.Contants;
import com.yysjyr.logisticssystem.common.util.DateTimeUtils;
import com.yysjyr.logisticssystem.common.util.IDUtils;
import com.yysjyr.logisticssystem.settings.entity.DicValue;
import com.yysjyr.logisticssystem.settings.entity.User;
import com.yysjyr.logisticssystem.settings.service.DicValueService;
import com.yysjyr.logisticssystem.settings.service.UserService;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/settings/qx/user/")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private DicValueService dicValueService;


    @RequestMapping("/toLogin")
    public ModelAndView toLogin(HttpServletRequest request,Model model){

        String loginAct = "";
        String loginPwd = "";
        String isRemPwd = "0";//没选中
        Cookie[] cookies = request.getCookies();
        if (cookies != null) {
            for (Cookie c : cookies) {
                if ("loginAct".equals(c.getName())) {
                    loginAct = c.getValue();
                } else if ("loginPwd".equals(c.getName())) {
                    loginPwd = c.getValue();
                }
            }
        }
        if (!("".equals(loginAct) || "".equals(loginPwd))) {
            isRemPwd = "1";
        }
        model.addAttribute("loginAct", loginAct);
        model.addAttribute("loginPwd", loginPwd);
        model.addAttribute("isRemPwd", isRemPwd);

        return new ModelAndView("/pages/settings/qx/user/login.html");
    }

    @RequestMapping("/toEnroll")
    public ModelAndView toEnroll(Model model){
        List<DicValue> dicValues = dicValueService.queryDicValuesByTypeCode("2");
        model.addAttribute("dicValues",dicValues);
        return new ModelAndView("/pages/settings/qx/user/enroll.html");
    }

    @RequestMapping("/enroll")
    public Object enroll(User user){
        ReturnObject returnObject = new ReturnObject();

        User user1 = userService.queryUserByUsername(user.getUsername());
        if (user1 != null){
            returnObject.setCode(Contants.RETURN_OBJECT_CODE_FAIL);
            returnObject.setMessage("用户已存在");
            return returnObject;
        }

        user.setId(IDUtils.getUUID());
        user.setType(dicValueService.queryFirstDicValueByTypeCode("1").getId());
        user.setLockUse(Contants.USER_LOCK_USE_DEFAULT);
        user.setCreateTime(DateTimeUtils.formatDateTime(new Date()));

        int i = userService.saveCreateUserByUser(user);

        if (i != 0) {
            returnObject.setCode(Contants.RETURN_OBJECT_CODE_SUCCESS);
        }else {
            returnObject.setCode(Contants.RETURN_OBJECT_CODE_FAIL);
            returnObject.setMessage("系统故障");
        }
        return returnObject;
    }

    @RequestMapping("login")
    public  Object login(String loginAct, String loginPwd, String isRemPwd, HttpServletRequest httpServletRequest, HttpServletResponse response, HttpSession session) {
        ReturnObject returnObject = new ReturnObject();
        Map<String, Object> map = new HashMap<>();
        map.put("loginAct", loginAct);
        map.put("loginPwd", loginPwd);
        User user = userService.queryUserByUsername(loginAct);
        if (user == null) {
            returnObject.setCode(Contants.RETURN_OBJECT_CODE_FAIL);
            returnObject.setMessage("用户名或者密码错误");
        } else {
            if(!user.getPassword().equals(loginPwd)){
                returnObject.setCode(Contants.RETURN_OBJECT_CODE_FAIL);
                returnObject.setMessage("用户名或者密码错误");
            } else {
                returnObject.setCode(Contants.RETURN_OBJECT_CODE_SUCCESS);
                session.setAttribute(Contants.SESSION_USER, user);

                if ("true".equals(isRemPwd)) {
                    Cookie cookie1 = new Cookie("loginAct", loginAct);
                    Cookie cookie2 = new Cookie("loginPwd", loginPwd);
                    cookie1.setMaxAge(10 * 24 * 60 * 60);
                    cookie2.setMaxAge(10 * 24 * 60 * 60);
                    response.addCookie(cookie1);
                    response.addCookie(cookie2);
                } else {
                    Cookie cookie1 = new Cookie("loginAct", "1");
                    Cookie cookie2 = new Cookie("loginPwd", "1");
                    cookie1.setMaxAge(0);
                    cookie2.setMaxAge(0);
                    response.addCookie(cookie1);
                    response.addCookie(cookie2);
                }
            }
        }
        return returnObject;
    }

    @RequestMapping("logout")
    public ModelAndView logout(HttpServletResponse response, HttpSession session){
        Cookie cookie1 = new Cookie("loginAct", "1");
        Cookie cookie2 = new Cookie("loginPwd", "1");
        cookie1.setMaxAge(0);
        cookie2.setMaxAge(0);
        response.addCookie(cookie1);
        response.addCookie(cookie2);
        session.invalidate();
        return new ModelAndView("/pages/index.html");
    }




}
